
$(function() {
	Tip.emp('员工');

	exten_check("#c_unconditional", "#exten_unconditional");
	exten_check("#c_unavailable", "#exten_unavailable");
	exten_check("#c_busy", "#exten_busy");	
		
	$("#c_unconditional").change(function() {		
		exten_toggle(this, "#exten_unconditional");
	});
	
	$("#c_unavailable").change(function() {		
		exten_toggle(this, "#exten_unavailable");
	});
	
	$("#c_busy").change(function() {
		exten_toggle(this, "#exten_busy");
	});
	
	$(".on_off :checkbox").iphoneStyle({checkedLabel: '开', uncheckedLabel: '关'});
	
	var exten = {
		source: function( request, response ) {
			$.ajax({
				url: 'emp.php?do=Exten.ajax',
				dataType: "json",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 12,
					q: request.term
				},
				success: function( data ) {
					response( $.map( data, function( item ) {
						return {
							label: item.exten + " [" + item.emp_t + ", " + item.dept_t + "]",
							value: item.exten
						}
					}));
				}
			});
		},
		
		minLength: 1
	};
	
	$($("input[id^='exten_']:visible")).autocomplete(exten);
});

function exten_toggle(c, exten) {
	if ($(c).attr("checked")) {
		$(exten).attr("disabled", false);
	} else {
		$(exten).attr("disabled", true);
	}
	
	exten_dh(exten);
}

function exten_dh(exten) {
	var id = $("#emp").val();
	
	var url = 'emp.php?do=Emp.ajax_id&id=' + id;
	// alert(url);
	$.getJSON(url, function(json) {
		// alert($.toJSON(json));
		var a = $(exten).val();
		if (is_null(a)) {
			$(exten).val(json.short);
		}
		
	});
}

function exten_check(c, exten) {
	var cf_ns = $(exten).val();
	if (cf_ns == null || cf_ns == '') {
		$(exten).attr("disabled", true);
	} else {
		$(exten).attr("disabled", false);
		$(c).attr("checked", true);
	}
}
